<?php
/**
 * date class
 * User: functions
 * Date: 2015/10/30
 * Time: 17:49
 */

namespace Tools\Date;


class Date
{
    protected $_date_format = 'Y-m-d H:i:s';
    protected $_week_days = 7;

    public function __construct()
    {

    }

    /**
     * 设置时间格式
     * @param $dateFormat
     */
    public function setDateFormat($dateFormat)
    {
        if($dateFormat) {
            $this->_date_format = $dateFormat;
        }
    }

    /**
     * 输出格式化日期
     * @param null $timestamp
     * @return bool|string
     */
    public function dateFormat($timestamp = null)
    {
        $timestamp = empty($timestamp) ? time() : $timestamp;
        return date($this->_date_format, $timestamp);
    }

    /**
     * 添加多少天
     * @param $amount
     * @param null $time
     * @param bool $timestamp
     * @return bool|int|string
     */
    public function addDays($amount, $time = null, $timestamp = false)
    {
        $time = $time ? $time : time();

        if(!is_numeric($amount) || $amount <= 0) {
            return $this->dateFormat($time);
        }

        $days = $amount;

        $nextDays = date($this->_date_format, mktime(23, 59, 59, date("m", $time), date("d", $time) + $days, date("Y", $time)));

        return $timestamp == true ? strtotime($nextDays) : $nextDays;
    }

    /**
     * 减少多少天
     *
     * @param $amount
     * @param null $time
     * @param bool $timestamp
     * @return bool|int|string
     */
    public function subDays($amount, $time = null, $timestamp = false)
    {
        $time = $time ? $time : time();

        if(!is_numeric($amount) || $amount <= 0) {
            return $this->dateFormat($time);
        }

        $days = $amount;

        $nextDays = date($this->_date_format, mktime(23, 59, 59, date("m", $time), date("d", $time) - $days, date("Y", $time)));

        return $timestamp == true ? strtotime($nextDays) : $nextDays;
    }

    /**
     * 比较2个日期相差多少天
     * @param $date1
     * @param $date2
     * @return float
     */
    public function computeDateDifferent($date1, $date2)
    {
        $timestamp1 = strtotime($date1);
        $timestamp2 = strtotime($date2);

        $diffTime = abs($timestamp2 - $timestamp1);
        $days = $diffTime / (24 * 3600);
        return $days;
    }

    /**
     * 比较2个小时相差多少时
     * @param $hour1
     * @param $hour2
     * @return mixed
     */
    public function computeHoursDifference($hour1, $hour2)
    {
        $diffTime = $hour2 - $hour1;
        return $diffTime;
    }

    /**
     * 获取日期的周数
     * @param $date
     * @return bool
     */
    public function getWeekOfDate($date)
    {
        if(empty($date)) {
            return false;
        }
        $weeks = ["日", "一", "二", "三", "四", "五", "六"];
        $position = date("w", strtotime($date));
        return $weeks[$position];
    }

    /**
     * 是合法的日期
     * @param $date
     * @return bool
     */
    public function isLegalDate($date)
    {
        return strtotime($date) === false ? false : true;
    }

    /**
     * 获取日期以后的一周
     * @param $date
     * @return array
     */
    public function getOneWeekTimeZone($date)
    {
        $days = $this->_week_days;
        $beginTime = strtotime($date);
        $timeZones = [];
        for($i = 0; $i < $days; $i++) {
            $nextDate = $this->addDays($i, $beginTime);
            $time = substr($nextDate, 0, 10);
            $temp = [];
            $temp['date'] = $time;
            $temp['week'] = "周" . $this->getWeekOfDate($time);
            $timeZones[] = $temp;
        }
        return $timeZones;
    }

    /**
     * 获取本周的一周日期
     * @return array
     */
    public function getThisWeekZone()
    {
        //获取本周第一天
        $firstDays = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("N") + 1, date("Y")));
        return $this->getOneWeekTimeZone($firstDays);
    }

    /**
     * 获取上周一周日期
     * @return array
     */
    public function getLastWeeKZone()
    {
        //获取上周第一天
        $firstDays = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), date("d") - date("N") + 1 - 7, date("Y")));
        return $this->getOneWeekTimeZone($firstDays);
    }

    /**
     * 获取本月日期
     * @return array
     */
    public function getThisMonth()
    {
        $firstDays = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m"), 1, date("Y")));
        $lastDays = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y")));

        return [$firstDays, $lastDays];
    }

    /**
     * 获取上个月
     * @return array
     */
    public function getLastMonth()
    {
        $firstDays = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m") - 1, 1, date("Y")));
        $lastDays = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), 0, date("Y")));

        return [$firstDays, $lastDays];
    }

    /**
     * 获取今年
     * @return array
     */
    public function getThisYear()
    {
        $firstDays = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 1, date("Y")));
        $lastDays = date("Y-m-d H:i:s", mktime(23, 59, 59, date("m"), date("t"), date("Y")));

        return [$firstDays, $lastDays];
    }

    /**
     * 获取去年
     * @return array
     */
    public function getLastYear()
    {
        $firstDays = date("Y-m-d H:i:s", mktime(0, 0, 0, 1, 1, date("Y") - 1));
        $lastDays = date("Y-m-d H:i:s", mktime(23, 59, 59, 1, 1, date("Y")));

        return [$firstDays, $lastDays];
    }

}